<?php namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\Tools;
use App\Plan;
use App\PlanJoin;
use App\User;
use App\UserInfo;
use EasyWeChat\Payment\Order;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;

class PlanController extends Controller
{
    public function joinPlan(Guard $guard,Request $request){
        $sex      = $request->get('sex');
        $industry = $request->get('industry');
        $industryTwo = $request->get('industry_two');
        $trade    = $request->get('trade');
        $jobName  = $request->get('job_name');
        $birthday = $request->get('birthday');
        $areaInfo = $request->get('area_info');

        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        /** @var User $user */
        $user = $guard->user();

        if(is_null($industry) || $industry == '')
            return response()->json(['level'=>'error','message'=>'请选择行业']);

        if(is_null($trade) || is_null($jobName) || $trade == '' || $jobName == '')
            return response()->json(['level'=>'error','message'=>'请选择职业']);

        if(is_null($birthday) || $birthday == '')
            return response()->json(['level'=>'error','message'=>'请选择生日']);

        if(is_null($areaInfo) || $areaInfo == '')
            return response()->json(['level'=>'error','message'=>'请选择城市']);

        /** @var Plan $thatPlan */
        $thatPlan = Plan::whereStatus(1)->first();
        if(!$thatPlan){
            return response()->json(['level'=>'error','message'=>'当前无开启的计划']);
        }

        $areaInfos = explode(" ",$areaInfo);

//        $planJoin = PlanJoin::whereUserId($user->id)->wherePlanId($thatPlan->id)->whereStatus(3)->whereIsPay(1)->first();
//        if(!$planJoin)
//            return response()->json(['level'=>'error','message'=>'当前无已付费的计划']);
//        $orderNum = Tools::makeBillNum();

//        $planJoin           = new PlanJoin();
//        $planJoin->order_num = $orderNum;
//        $planJoin->plan_id  = $thatPlan->id;
//        $planJoin->user_id  = $user->id;
//        $planJoin->sex      = $sex;
//        $planJoin->industry = $industry;
//        $planJoin->industry_two = $industryTwo;
//        $planJoin->trade    = $trade;
//        $planJoin->job_name = $jobName;
//        $planJoin->birthday = $birthday;
//        $planJoin->province = $areaInfos[0];
//        $planJoin->city     = $areaInfos[1];
//        $planJoin->area     = $areaInfos[2];
//        $planJoin->amount   = $thatPlan->amount;
//        $planJoin->status   = 1;
//        $planJoin->save();

        /** @var UserInfo $userinfo */
        $userinfo = UserInfo::whereUserId($user->id)->first();
        if($userinfo){
            if(!is_null($trade))$userinfo->trade = $trade;
            if(!is_null($jobName))$userinfo->job = $jobName;
            if(!is_null($birthday))$userinfo->birthday = $birthday;
            if(!is_null($areaInfos[0]))$userinfo->province = $areaInfos[0];
            if(!is_null($areaInfos[1]))$userinfo->city = $areaInfos[1];
            if(!is_null($areaInfos[2]))$userinfo->area = $areaInfos[2];
            if(!is_null($industry))$userinfo->industry = $industry;
            if(!is_null($industryTwo))$userinfo->industry_two = $industryTwo;
            if(!is_null($sex))$userinfo->sex = $sex;
            $userinfo->save();
        }

        return response()->json(['level'=>'success','message'=>'创建订单成功','data'=>$userinfo]);
    }

    public function startJoin(Guard $guard,Request $request){
        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录',"data"=>"no_login"]);

        /** @var User $user */
        $user = $guard->user();

        if($user->phone == "" || is_null($user->phone))
            return response()->json(['level'=>'error','message'=>'请先登录',"data"=>"no_bind_phone"]);

        /** @var Plan $thatPlan */
        $thatPlan = Plan::whereStatus(1)->first();
        if(!$thatPlan){
            return response()->json(['level'=>'error','message'=>'当前无开启的计划']);
        }

        $hasPlanJoin = PlanJoin::whereUserId($user->id)->wherePlanId($thatPlan->id)->whereStatus(1)->whereIsPay(1)->first();
        if($hasPlanJoin){
            return response()->json(['level'=>'error','message'=>'您已加入过计划']);
        }

        $orderNum = Tools::makeBillNum();

        $planJoin            = new PlanJoin();
        $planJoin->order_num = $orderNum;
        $planJoin->plan_id   = $thatPlan->id;
        $planJoin->user_id   = $user->id;
        $planJoin->amount   = $user->phone=="15999969373" ? 0.01 : $thatPlan->amount;
        $planJoin->status   = 1;
        $planJoin->save();

        return response()->json(['level'=>'success','message'=>'创建订单成功','data'=>$planJoin]);
    }
}